<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="openDia(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="tableData" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" size="small" type="text" @click="openDia(2,row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="lookPermission(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" title="添加" @close="closeDia">
        <!-- 表单内容 -->
        <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDia">取消</el-button>
            <el-button type="primary" @click="addPermission">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionAPI, addPermissionAPI, delPermissionAPI, lookPermissionAPI, editPermissionAPI } from '@/api/permission'
import { transListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      tableData: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [
          { required: true, message: '权限名称不可为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不可为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述不可为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermission()
  },
  methods: {
    // 查看权限的详情
    async lookPermission(id) {
      const { data } = await lookPermissionAPI(id)
      // console.log(data)
      this.formData = data
      this.openDia()
    },
    // 删除权限
    async delPermission(id) {
      await delPermissionAPI(id)
      this.$confirm('确认要删除吗', '警告').then(() => {
        this.$message.success('删除成功')
        this.getPermission()
      }).catch(() => {})
    },
    // 添加权限
    addPermission() {
      this.$refs.form.validate(async flag => {
        if (!flag) return
        await addPermissionAPI(this.formData)
        this.$message.success('添加成功')
        this.getPermission()
        this.showDialog = false
      })
    },
    openDia(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    closeDia() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '1', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 获取权限列表
    async getPermission() {
      const { data } = await getPermissionAPI()
      this.tableData = transListToTreeData(data, '0')
    }
  }
}
</script>
